package hedspi.k55.sm.model;

import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.util.Log;

public class XmlFetch extends DataFetch {

	private String mainURL = "http://www.google.com/ig/api?stock=";
	private Document document;

	public XmlFetch() {

	}

	public XmlFetch(String stockCode) {
		String forParsing;
		StringBuffer toGet = new StringBuffer(mainURL);
		toGet.append(stockCode);
		forParsing = getFromHTTP(toGet.toString());
		document = getDomElement(forParsing);
	}

	public void setStock(String stockCode) {
		String forParsing;
		StringBuffer toGet = new StringBuffer(mainURL);
		toGet.append(stockCode);
		forParsing = getFromHTTP(toGet.toString());
		document = getDomElement(forParsing);
	}

	public Document getDomElement(String xml) {
		Document doc = null;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {

			DocumentBuilder db = dbf.newDocumentBuilder();

			InputSource is = new InputSource();
			is.setCharacterStream(new StringReader(xml));
			doc = db.parse(is);

		} catch (ParserConfigurationException e) {
			Log.e("Error: ", e.getMessage());
			return null;
		} catch (SAXException e) {
			Log.e("Error: ", e.getMessage());
			return null;
		} catch (IOException e) {
			Log.e("Error: ", e.getMessage());
			return null;
		}
		// return DOM
		return doc;
	}

	// Parser Function
	public String getSymbol() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document.getElementsByTagName("symbol");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getPrettySymbol() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document.getElementsByTagName("pretty_symbol");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getCompanyName() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document.getElementsByTagName("company");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getExchange() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document.getElementsByTagName("exchange");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getExchangeTimeZone() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document
					.getElementsByTagName("exchange_timezone");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getUTCOffset() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document
					.getElementsByTagName("exchange_utc_offset");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getExchangeClosing() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document
					.getElementsByTagName("exchange_closing");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getDivisor() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document.getElementsByTagName("divisor");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getCurrency() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document.getElementsByTagName("currency");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public double getLast() {
		if (document == null)
			return 0;
		else {
			String res;
			NodeList nodeList = document.getElementsByTagName("last");
			res = nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue();
			return (Double.parseDouble(res));
		}
	}

	public double getHigh() {
		if (document == null)
			return 0;
		else {
			String res;
			NodeList nodeList = document.getElementsByTagName("high");
			res = nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue();
			return (Double.parseDouble(res));
		}
	}

	public double getLow() {
		if (document == null)
			return 0;
		else {
			String res;
			NodeList nodeList = document.getElementsByTagName("low");
			res = nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue();
			return (Double.parseDouble(res));
		}
	}

	public String getVolume() {
		if (document == null)
			return null;
		else {
			String res;
			NodeList nodeList = document.getElementsByTagName("volume");
			res = nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue();
			return res;
		}
	}

	public String getAvgVolume() {
		if (document == null)
			return null;
		else {
			String res;
			NodeList nodeList = document.getElementsByTagName("avg_volume");
			res = nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue();
			return res;
		}
	}

	public double getMarketCap() {
		if (document == null)
			return 0;
		else {
			String res;
			NodeList nodeList = document.getElementsByTagName("market_cap");
			res = nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue();
			return (Double.parseDouble(res));
		}
	}

	public double getOpen() {
		if (document == null)
			return 0;
		else {
			String res;
			NodeList nodeList = document.getElementsByTagName("open");
			res = nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue();
			return (Double.parseDouble(res));
		}
	}

	public double getYClose() {
		if (document == null)
			return 0;
		else {
			String res;
			NodeList nodeList = document.getElementsByTagName("y_close");
			res = nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue();
			return (Double.parseDouble(res));
		}
	}

	public double getChange() {
		if (document == null)
			return 0;
		else {
			String res;
			NodeList nodeList = document.getElementsByTagName("change");
			res = nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue();
			return (Double.parseDouble(res));
		}
	}

	public double getPercChange() {
		if (document == null)
			return -1;
		else {
			String res;
			NodeList nodeList = document.getElementsByTagName("perc_change");
			res = nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue();
			return (Double.parseDouble(res));
		}
	}

	public int getDelay() {
		if (document == null)
			return -1;
		else {
			String res;
			NodeList nodeList = document.getElementsByTagName("delay");
			res = nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue();
			return (Integer.parseInt(res));
		}
	}

	public String getTradeTimeStamp() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document
					.getElementsByTagName("trade_timestamp");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getTradeDateUTC() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document.getElementsByTagName("trade_date_utc");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getTimeUTC() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document.getElementsByTagName("trade_time_utc");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getCurrentDateUTC() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document
					.getElementsByTagName("current_date_utc");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getCurrentTimeUTC() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document
					.getElementsByTagName("current_time_utc");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public URL getChartURL() {
		if (document == null)
			return null;
		else {
			String res;
			StringBuffer baseURL = new StringBuffer("www.google.com");
			NodeList nodeList = document.getElementsByTagName("chart_url");
			res = nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue();
			baseURL.append(res);
			try {
				URL result = new URL(baseURL.toString());
				return result;
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
	}

	public String getISLDLast() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document.getElementsByTagName("isld_last");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getISLDTradeDateUTC() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document
					.getElementsByTagName("isld_trade_date_utc");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getISLDTradeTimeUTC() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document
					.getElementsByTagName("isld_trade_time_utc");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getBrutLast() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document.getElementsByTagName("brut_last");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getBrutTradeDateUTC() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document
					.getElementsByTagName("brut_trade_date_utc");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}

	public String getBrutTradeTimeUTC() {
		if (document == null)
			return null;
		else {
			NodeList nodeList = document
					.getElementsByTagName("brut_trade_time_utc");
			return (nodeList.item(0).getAttributes().getNamedItem("data")
					.getNodeValue());
		}
	}
}
